<template>
	<div>
    <zh-form ref="workflowClassFormRef" :span="24" :model="state.form"  :rules="state.rules">
      <zh-form-input
				prop="workflowClassCode"
        :disabled="props.showFlag||state.codeDisabled"
				label="分类编号"
				placeholder="请输入分类编号，不能重复"
				v-model="state.form.workflowClassCode"
			></zh-form-input>
      <zh-form-input
				prop="workflowClassTitle"
				label="分类名称"
				placeholder="请输入分类名称"
				v-model="state.form.workflowClassTitle"
        :disabled="props.showFlag"
			></zh-form-input>
      <zh-form-cascader
        prop="parentId"
        label="上级分类"
        placeholder="请选择"
        :props="{checkStrictly: true,emitPath:false}"
        v-model="state.form.parentId"
        :disabled="props.showFlag"
        :options="state.parentSelectTreeData"
      ></zh-form-cascader>
      <zh-form-select :disabled="props.showFlag" prop="workflowClassStatus" label="有效状态" :options="state.statusDictData" placeholder="请选择" v-model="state.form.workflowClassStatus"></zh-form-select>
      <zh-form-number
				prop="orderNo"
				label="排序号"
				placeholder="请输入排序号"
        :disabled="props.showFlag"
				v-model="state.form.orderNo"
			></zh-form-number>
      <zh-form-choose-org :disabled="props.showFlag" prop="orgId" label="所属组织" v-model="state.form.orgId"></zh-form-choose-org>
      <zh-form-cascader
        prop="areaCode"
        label="行政区划"
        placeholder="请选择"
        :props="{checkStrictly: true,emitPath:false}"
        v-model="state.form.areaCode"
        :disabled="props.showFlag"
        :options="state.areaCodeSelectTreeData"
      ></zh-form-cascader>
      <zh-form-select
        prop="appId"
        :lazy="true"
        :disabled="props.showFlag"
        label="所属应用"
        v-model="state.form.appId"
        :fieldNames="{ label: 'appName', value: 'appId' }"
        funcCode="application0012"
        searchField="appName"
      ></zh-form-select>   
      <zh-form-input :disabled="props.showFlag" :span="24" :rows="4" prop="remarks" type="textarea" label="备注" v-model="state.form.remarks"></zh-form-input> 
    </zh-form>
  </div>
</template>
<script lang="ts" setup name="WorkflowClassForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	classInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
  showFlag:{
    type:Boolean,
    default:false
  },
  modelValue: Boolean,
});
//------------------定义子组件---------------------------
const workflowClassFormRef=ref()
const emit = defineEmits(['update:modelValue']);
//------------------内部变量定义-------------------------
const state=reactive({
  form:{parentId:''},
  rules:{
    workflowClassCode: [
      {
        required: true,
        message: '请输入分类编号',
        trigger: 'blur',
      },
    ],
    workflowClassTitle: [
      {
        required: true,
        message: '请输入分类名称',
        trigger: 'blur',
      },
    ],
    workflowClassStatus: [
      {
        required: true,
        message: '请选择有效状态',
        trigger: 'blur',
      },
    ],
  },
  statusDictData:[{dictKey:'1',dictValue:'有效'},{dictKey:'0',dictValue:'无效'}],
  areaCodeSelectTreeData:[],
  parentSelectTreeData:[],
  codeDisabled:false,
})
//------------------定义调用方法-------------------------
function getAreaCodeData() {
	setTimeout(() => {
		$api.common.send.post('dict0026', {}).then((res) => {
			state.areaCodeSelectTreeData = res.result;
		});
	}, 20);
}
function getParentTree() {
	setTimeout(() => {
		$api.common.send.post('workflowClass0007', {}).then((res) => {
			state.parentSelectTreeData = res.result;
		});
	}, 20);
}
async function save(){
  //校验表单
	let checkFlag = await workflowClassFormRef.value.checkForm();
	if (checkFlag) {
    const info = state.form;
    let ret=null;
		if (state.codeDisabled) {
      ret=await $api.common.send.post('workflowClass0005',info);
      ret.saveType = 'edit';
      if(ret.code === 0){
        ElMessage.success('修改成功！');
        emit('update:modelValue', false);
      }
    } else {
      ret=await $api.common.send.post('workflowClass0004',info);	
      ret.saveType = 'add';
      if(ret.code === 0){
        ElMessage.success('新建成功！');
        emit('update:modelValue', false);
      }
		}
    return ret;
  }
	return null;
}
//------------------初始化------------------------------
onMounted(()=>{
  getAreaCodeData()
  getParentTree()
})
if (props.classInfo) {
  if(props.classInfo.formClassCode){
    state.codeDisabled=true
  }
  state.form=Object.assign({workflowClassStatus:'1'},props.classInfo)
}
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,save});
</script>
<style lang="scss" scoped></style>
